﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer

Public Class UserProfileControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If Trim(NameTextBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please enter profile name!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  [ProfileID] FROM dbo.UserProfile WHERE  ([ProfileName] = N'" & NameTextBox.Text.Trim & "') "
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Profile name already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New MasterDataLayer()
        UPro.Name = NameTextBox.Text
        If dataProvider.SaveUserProfile(UPro, ActionMode) = True Then
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub ClientEntryUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddNew()
    End Sub

    Dim UPro As UserProfile

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        NameTextBox.Text = String.Empty
        UPro = New UserProfile
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        UPro.ModulesList = dataProvider.GetModuleList(0)
        UserProfileDataGrid.ItemsSource = UPro.ModulesList
    End Sub

    Private Sub SelectAllButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        SetPermissionValue(True)
    End Sub

    Private Sub UnselectAllButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        SetPermissionValue(False)
    End Sub

    Private Sub SetPermissionValue(ByVal value As Boolean)
        For Each mpi In UPro.ModulesList
            mpi.Add = value
            mpi.Delete = value
            mpi.Edit = value
            mpi.Print = value
            mpi.View = value
        Next
    End Sub

    Private Sub SelectRowCheckBox_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim mi As ModulePermissions = TryCast(sender.DataContext, ModulePermissions)
        If mi IsNot Nothing Then
            If DirectCast(sender, System.Windows.Controls.CheckBox).IsChecked Then
                mi.Add = True
                mi.Delete = True
                mi.Edit = True
                mi.Print = True
                mi.View = True
            Else
                mi.Add = False
                mi.Delete = False
                mi.Edit = False
                mi.Print = False
                mi.View = False
            End If
        End If

    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetUserProfile
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
